{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3862992f-7624-454e-9488-6affd9a835f8",
   "metadata": {},
   "source": [
    "# 通过openai api调用大模型的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "baec2bae-d5c2-4c57-88f3-bef960ba59c6",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 准备运行环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b6c3377f-dbdf-496a-a562-13ab6dea0ba5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: openai in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (0.27.4)\n",
      "Requirement already satisfied: tqdm in /home/shanyou/.local/lib/python3.10/site-packages (from openai) (4.65.0)\n",
      "Requirement already satisfied: requests>=2.20 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from openai) (2.29.0)\n",
      "Requirement already satisfied: aiohttp in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from openai) (3.8.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from requests>=2.20->openai) (3.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from requests>=2.20->openai) (1.26.15)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from requests>=2.20->openai) (2022.12.7)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from requests>=2.20->openai) (3.1.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from aiohttp->openai) (1.3.3)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from aiohttp->openai) (1.3.1)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from aiohttp->openai) (4.0.2)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from aiohttp->openai) (1.9.2)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from aiohttp->openai) (6.0.4)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /home/shanyou/anaconda3/envs/chatglm/lib/python3.10/site-packages (from aiohttp->openai) (23.1.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install openai"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab5088cc-fa95-42b3-a580-6ef54437c359",
   "metadata": {},
   "source": [
    "## 初始化环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "609868e9-c372-4639-930e-03fd533cebe6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!export OPENAI_API_KEY=\"token1\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "490ef4e9-6204-46b5-90d0-62b42577c30f",
   "metadata": {},
   "source": [
    "## 配置模型文件\n",
    "```config.yaml\n",
    "models:\n",
    "  - name: fake\n",
    "    mtype: fake\n",
    "  - name: chinese-alpaca-plus-lora-7b\n",
    "    check_point_path: /home/model_root/llama/chinese_alpaca_plus_lora_7b_all/7B/ggml-model-q4_0.bin\n",
    "    mtype: llama\n",
    "  - name: chatglm-6b\n",
    "    check_point_path: /home/model_root/chatglm-6b\n",
    "    mtype: glm\n",
    "verbose: true\n",
    "llama:\n",
    "  max_context_size: 2048\n",
    "```\n",
    "其中name: 模型的名称，在调用openai的api时需要要与之对应。\n",
    "\n",
    "check_point_path: 文件名或者目录，对此应这需要加载的模型文件\n",
    "\n",
    "mtype: 模型加载的方式，目前只支持llama.cpp的cpu加载和glm的GPU加载。 glm加载使用accelerate, 优先使用GPU, GPU不够再用CPU, 默认使用每颗GPU80%的显存用来推理。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a9b5411-82ac-4f47-bdb7-3554940af8ab",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 运行python启动模型api\n",
    "\n",
    "```bash\n",
    "python main.py --config tests/config.yaml\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ebc1e01-37ec-4ee5-87fe-63a59ad8cdf9",
   "metadata": {},
   "source": [
    "## 构建API请求\n",
    "参考[openai-cookbook](https://github.com/openai/openai-cookbook)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "23eefd08-3c5e-4fb4-a5bd-dca0a4c6e0f0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "model name defaults(chinese-alpaca-plus-lora-7b): chatglm-6b\n"
     ]
    }
   ],
   "source": [
    "import openai, time\n",
    "_api_key = \"token1\"\n",
    "openai.api_key  = _api_key\n",
    "openai.api_base = \"http://localhost:8080/v1\"\n",
    "model_name=input(\"model name defaults(chinese-alpaca-plus-lora-7b):\") or \"chinese-alpaca-plus-lora-7b\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "486282ad-7534-4cfc-8e15-da7216c1574e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Full response received 36.25 seconds after request\n",
      "Full response received:\n",
      "{\n",
      "  \"choices\": [\n",
      "    {\n",
      "      \"finish_reason\": \"length\",\n",
      "      \"index\": 0,\n",
      "      \"message\": {\n",
      "        \"content\": \"Sure, I can do that for you. Here's the count to 100 with a comma between each number:\\n\\n9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100.\",\n",
      "        \"role\": \"assistant\"\n",
      "      }\n",
      "    }\n",
      "  ],\n",
      "  \"created\": 1684041320,\n",
      "  \"id\": \"chatcmpl-27c6bd33-d10e-4395-81e1-bba5c5dd1417\",\n",
      "  \"model\": \"chatglm-6b\",\n",
      "  \"object\": \"chat.completion\",\n",
      "  \"usage\": {\n",
      "    \"completion_tokens\": 1,\n",
      "    \"prompt_tokens\": 1,\n",
      "    \"total_tokens\": 2\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "start_time = time.time()\n",
    "\n",
    "# send a ChatCompletion request to count to 100\n",
    "response = openai.ChatCompletion.create(\n",
    "    model=model_name,\n",
    "    messages=[\n",
    "        {'role': 'user', 'content': 'Count to 100, with a comma between each number and no newlines. E.g., 1, 2, 3, ...'}\n",
    "    ],\n",
    "    temperature=0.1,\n",
    ")\n",
    "\n",
    "# calculate the time it took to receive the response\n",
    "response_time = time.time() - start_time\n",
    "\n",
    "# print the time delay and text received\n",
    "print(f\"Full response received {response_time:.2f} seconds after request\")\n",
    "print(f\"Full response received:\\n{response}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2293009c-7ef8-400c-814b-607c23fbe869",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatgpt",
   "language": "python",
   "name": "chatgpt"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
